package com.pucheng.bracelet.ui.activity.sao2

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.database.repository.Sao2Repository
import com.pucheng.bracelet.app.util.StatusBarUtil
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.data.bean.BarChartBean
import com.pucheng.bracelet.databinding.ActivitySao2Binding
import com.pucheng.bracelet.viewmodel.sao2.Sao2ViewModel
import com.pucheng.bracelet.viewmodel.sao2.WordViewModelFactory
import kotlinx.android.synthetic.main.activity_blood_pressure.*
import kotlinx.android.synthetic.main.activity_sao2.*
import kotlinx.android.synthetic.main.activity_sao2.btn_start_measuring
import kotlinx.android.synthetic.main.activity_sao2.layout_title
import kotlinx.android.synthetic.main.activity_sao2.tb_hint
import kotlinx.android.synthetic.main.layout_sao2_vaue_progress.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.demo.app.base.BaseActivity

class Sao2Activity : BaseActivity<BaseViewModel, ActivitySao2Binding>() {
    private val sao2ViewModel: Sao2ViewModel by viewModels { WordViewModelFactory(Sao2Repository((application as App).database.sao2Dao())) }
    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.vm = sao2ViewModel
        tv_title.text = getString(R.string.sao2)
        layout_title.setBackgroundResource(R.color.sao2_bg)
        StatusBarUtil.setColor(this, resources.getColor(R.color.sao2_bg), 0)
        btn_start_measuring.setOnClickListener {
            sao2ViewModel.measuredSao2()
        }
        ibtn_back.click {
            finish()
        }
        ibtn_right.click { startActivity(Intent(this, Sao2HistoryActivity::class.java)) }
//        sao2ViewModel

        tb_hint.setTitle("近7次血氧饱和度测量趋势")
    }

    override fun createObserver() {
        super.createObserver()
        sao2ViewModel.apply {
            sao2.observe(this@Sao2Activity, Observer {
                mDatabind.tvSao2Num.text = "${it.sao2Vaule}%"
                mDatabind.tvSao2UpdataDate.text = it.measuredDate
                pb_sao2.progress = (it.sao2Vaule - 80)*5
            })
            allSao2To7Day.observe(this@Sao2Activity, Observer {
                it.let {

                    tb_hint.showBarChart(it, resources.getColor(R.color.sao2_bg))
                }
            })
        }
    }

    override fun layoutId(): Int = R.layout.activity_sao2
}